﻿using Common;
using Panuon.UI.Silver;
using System;
using System.Windows;
using Service.Implements;
using Service.DtoModel.DeskApp;

namespace RunApp.PartialViews.Business
{
    /// <summary>
    /// 类型编辑
    /// </summary>
    public partial class EditType : WindowX
    {
        #region Property       
        SysTypeService TypeService = new SysTypeService();

        SysType data;
        #endregion
        public EditType(SysType data)
        {
            InitializeComponent();
            this.data = data;
            txtName.Text = data.typeName;
            switch (data.typeGroup)
            {
                case 1:
                    txtType.Text = "餐桌类型";
                    break;
                case 2:
                    txtType.Text = "菜品类型";
                    break;
                case 3:
                    txtType.Text = "员工类型";
                    break;
                default: break;
            }            
        }
        public EditType()
        {
            InitializeComponent();
        }
        #region Event
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnMin_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }
        /// <summary>
        /// 最大化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnMax_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Maximized) WindowState = WindowState.Normal;
            else WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            var _res = Save();
            if (_res == (int)ApiEnum.Status) Close();
        }
        #endregion

        #region Function
        private int Save()
        {
            var _res = (int)ApiEnum.Error;
            try
            {
                var _rname = txtName.Text.Trim();
                if (string.IsNullOrEmpty(_rname))
                {
                    Notice.Show("请录入类型名称！", "提示", 3, MessageBoxIcon.Warning);
                    return _res;
                }
                data.typeName = _rname;
                data.lastUpDate = DateTime.Now;
                var _addRes = TypeService.UpdateAsync(data).Result;
                if (_addRes.statusCode == (int)ApiEnum.Status)
                {
                    Notice.Show("编辑成功！", "提示", 3, MessageBoxIcon.Success);
                }
                else
                {
                    Notice.Show("编辑失败！", "提示", 3, MessageBoxIcon.Error);
                    Logger.Default.ProcessError(_addRes.statusCode, "编辑类型失败");
                }
                _res = _addRes.statusCode;
            }
            catch (Exception ex)
            {
                Logger.Default.Error("编辑类型出现异常", ex);
            }
            return _res;
        }
        #endregion
    }
}
